package deletetask

import (
	"github.com/astaxie/beego/logs"
	"io/ioutil"
	"os"
	"time"
)

func DelDir(filePath string) {

	fileList, err := ioutil.ReadDir(filePath)
	if err != nil {
		logs.Error("ReadDir error:", filePath, err.Error())
		return
	}

	for _, file := range fileList {
		fileName := file.Name()
		if file.IsDir() {
			childFilePath := filePath + fileName + "/"
			childFileList, iErr := ioutil.ReadDir(childFilePath)
			if iErr != nil {
				logs.Error("ReadDir error:", filePath, iErr.Error())
			}
			if len(childFileList) == 0 {
				logs.Info("this is an empty folder", childFilePath)
				iErr = os.Remove(childFilePath)
				if iErr != nil {
					logs.Error("remove empty folder filePath error:", childFilePath, iErr.Error())
				}
				return
			}
			DelDir(childFilePath)
		}
		now := time.Now()
		if now.Unix()-60*60*24*30 > file.ModTime().Unix() {
			removePath := filePath + fileName
			err = os.Remove(removePath)
			if err != nil {
				logs.Error("remove file removePath error:", err.Error())
			}
			logs.Info(removePath, "is remove at", now)
		}

	}

}
